import Vue from 'vue'
import VueRouter from 'vue-router'
import store from "@/store"

const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location, onResolve, onReject) {
  if (onResolve || onReject) return originalPush.call(this, location, onResolve, onReject)
  return originalPush.call(this, location).catch(err => err)
}


Vue.use(VueRouter)

const routes = [
  {
    // 默认布局页面，即下面是一个tarBar，上面是2级路由的视图
    path: '/',
    component: () => import('../views/Layout/index.vue'),
    children: [
      {
        // 默认home页面
        path: '',
        name: "Home",
        component: () => import('../views/home/index.vue'),
        meta: { isAuthorization: false, title: "悟空租车" }
      },
      {
        // 访问/home跳转到首页
        path: '/home',
        redirect: '/'
      },
      {
        // 客服页面
        path: '/service',
        name: 'Service',
        component: () => import('../views/service/index.vue'),
        meta: { isAuthorization: false, title: "客服" }
      },
      {
        // 订单页面
        path: '/order',
        name: 'Order',
        component: () => import('../views/order/index.vue'),
        meta: { isAuthorization: true, title: "订单" }
      },
      {
        // 我的页面
        path: '/my',
        name: 'My',
        component: () => import('../views/my/index.vue'),
        meta: { isAuthorization: false, title: "我的" }
      },
    ]
  },
  {
    // 租车指南页面
    path: '/carGuide',
    name: 'CarGuide',
    component: () => import('../views/CarGuide/index.vue'),
    meta: { isAuthorization: false, title: "租车指南" }
  },
  {
    // 常见问题页面
    path: '/commonProblem',
    name: 'CommonProblem',
    component: () => import('../views/commonProblem/index.vue'),
    meta: { isAuthorization: false, title: "常见问题" }
  },
  {
    // 8重品台保障页面
    path: "/platformGuarantee",
    name: 'PlatformGuarantee',
    component: () => import("../views/home/PlatformGuarantee.vue"),
    meta: { isAuthorization: false, title: "平台保障" }
  },
  {
    // 车辆分类页面
    path: "/selectCarList",
    name: 'SelectCarList',
    component: () => import("../views/selectCarList/index.vue"),
    meta: { isAuthorization: false, title: "选车列表" }
  },
  {
    // 订单页面
    path: "/orderCar",
    name: 'OrderCar',
    component: () => import("../views/OrderCar/index.vue"),
    meta: { isAuthorization: true, title: "订单" }
  },
  {
    // 自驾心得页面
    path: "/selfDrivingExperience",
    name: "SelfDrivingExperience",
    component: () => import("../views/SelfDrivingExperience"),
    meta: { isAuthorization: false, title: "自驾心得" }
  },
  {
    // 支付页面
    path: "/payCarRent",
    name: "PayCarRent",
    component: () => import("../views/PayCarRent/index.vue"),
    meta: { isAuthorization: true, title: "收银台" }
  },
  {
    // 登录页面
    path: "/login",
    name: "Login",
    component: () => import("../views/my/components/Login.vue"),
    meta: { isAuthorization: false, title: "登录" }
  },
  {
    // 个人信息页面
    path: "/profile",
    name: "Profile",
    component: () => import("../views/my/components/Profile.vue"),
    meta: { title: "个人信息" }
  }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

// 全局路由拦截器
router.beforeEach((to, from, next) => {
  document.title = to.meta.title
  const token = store.state.token
  if (!token && to.meta.isAuthorization && to.name !== 'Login') {
    store.commit("changePrePage",to.path)
    next({ name: "Login" })
  } else {
    next()
  }
})

export default router
